package com.example.smartrefreshlayoutdemo.adapter;

import android.database.DataSetObservable;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListAdapter;

import androidx.annotation.LayoutRes;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearSmoothScroller;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

public abstract class BaseRecyclerAdapter<T> extends RecyclerView.Adapter<SmartViewHolder> implements ListAdapter {
    private int mLayoutId;
    private List<T> mList;
    private int mLastPosition = -1;
    private boolean openAnimationEnable = true;
    protected AdapterView.OnItemClickListener mListener;

    public BaseRecyclerAdapter(int mLayoutId) {
        setHasStableIds(false);
        this.mLayoutId = mLayoutId;
        this.mList = new ArrayList<>();
    }

    public BaseRecyclerAdapter(Collection<T> collection, @LayoutRes int layoutId) {
        setHasStableIds(false);
        this.mList = new ArrayList<>(collection);
        this.mLayoutId = layoutId;
    }
    public BaseRecyclerAdapter(Collection<T> collection,@LayoutRes int layoutId,AdapterView.OnItemClickListener mListener){
        setHasStableIds(false);
        this.mList = new ArrayList<>(collection);
        this.mLayoutId = layoutId;
        setOnItemClickListener(mListener);

    }

    @NonNull
    @Override
    public SmartViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new SmartViewHolder(LayoutInflater.from(parent.getContext()).inflate(mLayoutId,parent,false),mListener);
    }

    @Override
    public void onBindViewHolder(@NonNull SmartViewHolder holder, int position) {
        onBindViewHolder(holder,position < mList.size() ? mList.get(position) : null,position);
    }

    public abstract void onBindViewHolder(SmartViewHolder holder,T model,int position);

    @Override
    public int getItemCount() {
        return mList.size();
    }

    @Override
    public boolean areAllItemsEnabled() {
        return true;
    }

    @Override
    public boolean isEnabled(int position) {
        return true;
    }

    private final DataSetObservable mDataSetObservable = new DataSetObservable();
    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        mDataSetObservable.registerObserver(observer);
    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        mDataSetObservable.unregisterObserver(observer);
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        SmartViewHolder holder;
        if (convertView != null) {
            holder = (SmartViewHolder) convertView.getTag();
        }else{
            holder = onCreateViewHolder(parent,getItemViewType(position));
            convertView = holder.itemView;
            convertView.setTag(holder);
        }
        holder.setPosition(position);
        onBindViewHolder(holder,position);
        addAnimate(holder,position);
        return convertView;
    }

    @Override
    public int getViewTypeCount() {
        return 0;
    }

    @Override
    public boolean isEmpty() {
        return getCount() == 0;
    }

    @Override
    public void onViewAttachedToWindow(@NonNull SmartViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        addAnimate(holder,holder.getLayoutPosition());
    }

    public BaseRecyclerAdapter setOnItemClickListener(AdapterView.OnItemClickListener listener){
        this.mListener = listener;
        return this;
    }

    private void addAnimate(SmartViewHolder holder,int position){
        if (openAnimationEnable && mLastPosition < position) {
            holder.itemView.setAlpha(0);
            holder.itemView.animate().alpha(1).start();
            mLastPosition = position;
        }
    }

    //刷新数据，先清空再添加
    public BaseRecyclerAdapter<T> refresh(Collection<T> collection){
        mList.clear();
        mList.addAll(collection);
        notifyDataSetChanged();
        mDataSetObservable.notifyChanged();
        mLastPosition = -1;
        return this;
    }

    //加载更多
    public BaseRecyclerAdapter<T> loadMore(Collection<T> collection){
        mList.addAll(collection);
        notifyDataSetChanged();
        mDataSetObservable.notifyChanged();
        return this;
    }

    public BaseRecyclerAdapter<T> insert(Collection<T> collection){
        mList.addAll(0,collection);
        notifyItemRangeInserted(0,collection.size());
        mDataSetObservable.notifyChanged();
        return this;
    }
}
